package com.yuanian.timingwheel.annotation;

import com.yuanian.timingwheel.context.SpringContext;
import com.yuanian.timingwheel.model.task.build.TaskBuilder;
import com.yuanian.timingwheel.model.task.build.TaskExecutionBuilder;
import java.lang.reflect.Method;
import java.util.Date;

/**
 * TimingTask注解处理器
 */
public abstract class TimingTaskAnnoHandler {

    public TaskBuilder handle(String dynamicParamSolver, Class clz, Method method, Object obj, Object[] args, TimingTaskType type) {
        try {
            Method dynamicParamMethod = clz.getDeclaredMethod(dynamicParamSolver, method.getParameterTypes());
            TaskExecutionBuilder taskExecutionBuilder = (TaskExecutionBuilder)dynamicParamMethod.invoke(obj, args);
            return new TaskBuilder() {
                @Override
                public String executionMethod() {
                    return method.getName();
                }

                @Override
                public String beanName() {
                    return SpringContext.getBeanName(clz);
                }

                @Override
                public Object[] args() {
                    return args;
                }

                @Override
                public long executionInterval() {
                    return taskExecutionBuilder.executionInterval();
                }

                @Override
                public long executionRounds() {
                    return taskExecutionBuilder.executionRounds();
                }

                @Override
                public Date executionTime() {
                    return taskExecutionBuilder.executionTime();
                }

                @Override
                public long executedRounds() {
                    return taskExecutionBuilder.executedRounds();
                }

                @Override
                public String taskDesc() {
                    return taskExecutionBuilder.taskDesc();
                }

                @Override
                public String businessId() {
                    return taskExecutionBuilder.businessId();
                }

                @Override
                public TimingTaskType type() {
                    return type;
                }
            };
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
}
